package com.spider.admin.controller;
import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.service.cloud.CloudService;
import com.spider.admin.store.primary.model.CloudInfo;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/cloud")
public class CloudController {

    @Resource
    private CloudService cloudService;

    /**
     * 新增服务云信息
     *
     * @param cloudInfo
     * @return
     * @throws SpiderException
     */
    @PostMapping("/add")
    public int saveCloud(@Validated(CloudInfo.Save.class) @RequestBody CloudInfo cloudInfo) throws SpiderException {
        return cloudService.saveCloudInfo(cloudInfo);
    }


    /**
     * 更新服务云信息
     *
     * @param cloudInfo
     * @param pid
     * @return
     * @throws SpiderException
     */
    @PutMapping("/update/{pid}")
    public int updateCloudById(@Validated(CloudInfo.Update.class) @RequestBody CloudInfo cloudInfo, @NotEmpty(message = "更新ID不能为空") @PathVariable("pid") String pid) throws SpiderException {
        return cloudService.updateCloudById(cloudInfo, pid);
    }


    /**
     * 查询服务云信息
     *
     * @param pid
     * @return
     * @throws SpiderException
     */
    @GetMapping("/query/{pid}")
    public CloudInfo queryCloudById(@PathVariable("pid") @NotEmpty(message = "查询ID不能为空") String pid) throws SpiderException {
        return cloudService.queryCloudById(pid);
    }

    /**
     * 激活服务云
     *
     * @return
     */
    @PutMapping("/active/{pid}")
    public int activeCloudById(@PathVariable("pid") @NotEmpty(message = "ID参数不能为空") String pid) throws SpiderException {
        return cloudService.activeCloud(pid);
    }

    /**
     * 归档服务云
     *
     * @return
     */
    @PutMapping("/archive/{pid}")
    public int archiveCloudById(@PathVariable("pid") @NotEmpty(message = "ID参数不能为空") String pid) throws SpiderException {
        return cloudService.archiveCloud(pid);
    }

    /**
     * 查询租户服务云清单
     * @param tenantId
     * @param page
     * @param pageSize
     * @param cloudName
     * @param cloudBasePath
     * @param archiveStatus
     * @return
     * @throws SpiderException
     */
    @GetMapping("/list/{tenantId}")
    public PageResult<CloudInfo> queryClientListByTenantId(
            @PathVariable String tenantId,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String cloudName,
            @RequestParam(required = false) String cloudBasePath,
            @RequestParam(required = false) String archiveStatus)
            throws SpiderException{
        return cloudService.queryCloudByTenantId(tenantId, page, pageSize, cloudName, cloudBasePath, archiveStatus);
    }
}

